package beans;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import database.pack.DatabaseConnector;

public class NewItemBean implements Serializable 
{
	private static final long serialVersionUID = 1L;
	private static int itemId;
	
	public void addNewItem ( String category,
								 String description,
								 String itemWeight,
								 String price,
								 String quantityInStock )
	{
		try
		{
			DatabaseConnector databaseConnect = new DatabaseConnector();
			databaseConnect.connectToDatabase();
			Statement stat = databaseConnect.getConnection().createStatement();
			ResultSet idResult = stat.executeQuery("SELECT id FROM product");
			
			while(idResult.next())
			{
				itemId = idResult.getInt("id") + 1;
			}
			
			stat.executeUpdate(
								"INSERT INTO product" + 
								"(id, category, description, itemWeight, price, quantityInStock)" +
								"VALUES (" + 
											itemId + ","	+ 
											"'" + category 			+ "'," + 
											"'" + description		+ "'," + 
											"'" + itemWeight		+ "'," + 
											"'" + price				+ "'," + 
											"'" + quantityInStock	+ "')"); 
			
			stat.close();
			databaseConnect.getConnection().close();
		}
		catch(SQLException exception)
		{
			System.out.println(exception.getMessage());
		}
		catch(ClassNotFoundException e)
		{
			System.out.println("Couldn't connect");
			e.printStackTrace();
		}
	}
}
